<template>
    <div @click="goPfo" class="pfo-fn">
        <slot @shareUrl="goPfo"></slot>    
    </div>
</template>
<script>
// 分享代付链接组件
import getPOFurl from '../../utils/outUrl/getPayForOthersUrl'
import {Toast} from "../../utils/toast";
import payForOthers from '../../utils/outUrl'
export default {
  props: {
      //订单号  
      orderNo: String
  },
  methods: {
    goPfo() {
        this.payForOthersClick()
    },
    async payForOthersClick() {
        console.log('----------------------')
        console.log('当前代付订单号',this.orderNo)
        console.log('----------------------')
        const response = await getPOFurl(this.orderNo)
        if (response.data.returnCode === 1) {
            if (response.data.data[0]) {
                this.payForOthersDo(response.data.data[0])
            } else {
                Toast({
                    duration: 1000,
                    type: 'fail',
                    message: response.data.msg
                })
            }
        } else {
            Toast({
                duration: 1000,
                type: 'fail',
                message: response.data.msg
            })
        }
        

    },
    payForOthersDo(url) {
        const param = this.getParam(url)
        const pfoUrl = url.split('=')[0] +'='+ encodeURIComponent(param)
        console.log('----------------------')
        console.log('生成的代付链接：',pfoUrl)
        console.log('----------------------')
        payForOthers({
            "title": "宁家商城代付",
            "content": "您的好友在宁家鲜生上选购了好商品，快去帮忙付个款吧！",
            "url": pfoUrl,
            "success": () => {
                // Toast({
                //     duration: 1000,
                //     type: "success",
                //     message: "请求代付成功"
                // });
                // 点击留在微信再转回app，不会触发success函数
                this.$router.replace({path: 'order', query: {orderStatus: 1}})
            },
            "error": () => {
                // Toast({
                //     duration: 1000,
                //     type: 'fail',
                //     message: '请求代付失败'
                // })
            }
        })
        
    },
    // 加密url里面包含等号
    getParam(url) {
        let query = url.split('?')[1]
        let param = ''
        let flg = false
        for (let i = 0; i < query.length; i++) {
            if (query[i]=='=') {
                flg = true
            }
            if (flg) {
                param += query[i]
            }
            
        }
        return param.substr(1)
    }
  }
}
</script>
<style>
    .pfo-fn {
        margin-top: 10px;
    }
</style>
